package com.hongkzh.www.sys;

import com.hongkzh.www.conmon.AuthInterceptor;
import com.hongkzh.www.conmon.FileHandler;
import com.hongkzh.www.conmon.MyRenderFactory;
import com.hongkzh.www.conmon.RedisTool;
import com.hongkzh.www.routes.V1Routes;
import com.hongkzh.www.swagger.config.routes.SwaggerRoutes;
import com.jfinal.config.*;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

import java.io.File;
import java.util.List;
import java.util.Set;

/**
 * @author martins
 */
public class SystemConfig  extends JFinalConfig {
    @Override
    public void afterJFinalStart() {
        //下拉数据
        RedisTool.del("dict");
        List<Record> list= Db.find(Db.getSqlPara("adv.getDicts"));
        for (Record r:list){
            RedisTool.hset(r.get("type")+":"+r.get("id"),"label",r.get("label"));
            RedisTool.sadd("dict",r);
        }
       //国家
        RedisTool.del("area");
        List<Record> area= Db.find(Db.getSqlPara("adv.getArea"));
        for (Record r:area){
            RedisTool.hset("area:"+r.get("id"),"name",r.get("name"));
            //RedisTool.hset("area:"+r.get("id"),"label",r.get("name"));
            RedisTool.hset("area:"+r.get("id"),"parentId",r.get("parentId"));
            RedisTool.sadd("area",r);
        }
       // RedisTool.expire("area",60*60);

        //RedisTool.expire("dict",60*60);
        //招聘职业类别
        RedisTool.del("recruitCategorys");
        List<Record> recruitCategorys=Db.find(Db.getSqlPara("adv.getRecruitCategorys"));
        for (Record r:recruitCategorys){
            RedisTool.hset("recruitCategory:"+r.get("id"),"name",r.get("name"));

            RedisTool.sadd("recruitCategorys",r);
        }
        //招聘行业类别
        RedisTool.del("recruitIndustrys");
        List<Record> recruitIndustrys=Db.find(Db.getSqlPara("adv.getRecruitIndustrys"));
        for (Record r:recruitIndustrys){
            RedisTool.hset("recruitIndustry:"+r.get("id"),"name",r.get("name"));

            RedisTool.sadd("recruitIndustrys",r);
        }

         Set<byte[]> set=RedisTool.getSmembers("recruitCategorys");
        for (byte[] str : set) {
            System.out.println(RedisTool.valueFromBytes(str));
        }
        //RedisTool.expire("recruitCategorys",60*60);
        //所有分类
        RedisTool.del("allCategorys");
        List<Record> allCategorys=Db.find(Db.getSqlPara("adv.ALLCategoryList"));
        for (Record r:allCategorys){
            String imgSrc = r.getStr("imgSrc");
            if (StrKit.notBlank(imgSrc)&&imgSrc.indexOf("http")<0) {
                r.set("imgSrc", PropKit.get("fileServer") + imgSrc);
            }
            RedisTool.sadd("allCategorys",r);
        }

        //RedisTool.expire("allCategorys",60*60);

        //系统电商分类
        RedisTool.del("systemShopCategorys");
        List<Record> systemShopCategorys=Db.find(Db.getSqlPara("adv.systemShopCategorys"));
        for (Record r:systemShopCategorys){
            RedisTool.hset("systemShopCategory:"+r.get("id"),"name",r.get("name"));

            RedisTool.sadd("systemShopCategorys",r);
        }
        //自媒体快递地区
        RedisTool.del("mediaAreas");
        List<Record> mediaRegions=Db.find(Db.getSqlPara("adv.mediaRegion"));
        for (Record r:mediaRegions){
            //RedisTool.hset("mediaRegion:"+r.get("id"),"name",r.get("name"));
            RedisTool.sadd("mediaAreas",r);
        }

        List<Record> mediaAreas=Db.find(Db.getSqlPara("adv.mediaAreas"));
        for (Record r:mediaAreas){
               r.set("name",RedisTool.hgetArea(r.getStr("id")));
                RedisTool.sadd("mediaAreas",r);
        }
        //自媒体电商分类
        RedisTool.del("allMediaShopCategorys");
        List<Record> allMediaShopCategorys=Db.find(Db.getSqlPara("adv.allMediaShopCategorys"));
        for (Record r:allMediaShopCategorys){
            RedisTool.hset("allMediaShopCategory:"+r.get("mediaCategoryId"),"name",r.get("name"));

            RedisTool.sadd("allMediaShopCategorys",r);
        }


        //快递公司
        RedisTool.del("shopexpresss");
        List<Record> shopexpress=Db.find(Db.getSqlPara("shop.shopexpress"));
        for (Record r:shopexpress){
            RedisTool.hset("shopexpress:"+r.get("name"),"abbreviation",r.get("abbreviation"));
            RedisTool.sadd("shopexpresss",r);
        }
        super.afterJFinalStart();
    }

    @Override
    public void configConstant(Constants me) {

        PropKit.use("a_little_config.txt");
        me.setDevMode(PropKit.getBoolean("devMode", false));
        me.setBaseUploadPath(PropKit.get("userfiles.basedir"));
        me.setMaxPostSize(104857600);
        me.setBaseDownloadPath(PropKit.get("userfiles.basedir"));
         me.setRenderFactory(new MyRenderFactory());
        //PathKit.
    }
    @Override
    public void configRoute(Routes me) {

        me.add(new V1Routes());
        me.add(new SwaggerRoutes());

    }
    @Override
    public void configEngine(Engine me) {
        //me.addSharedMethod(new StrKit());
        //me.addSharedObject("sk",new com.jfinal.kit.StrKit());
    }
    @Override
    public void configPlugin(Plugins me) {
        // 配置 druid 数据库连接池插件
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"), PropKit.get("jdbc.password").trim());
        me.add(druidPlugin);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        //arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        arp.setBaseSqlTemplatePath(PathKit.getRootClassPath()+ File.separator+"sql");
        arp.setDevMode(true);
        arp.setShowSql(true);
        arp.addSqlTemplate("all.sql");
        arp.getEngine().addSharedObject("sk",new com.jfinal.kit.StrKit());
        me.add(arp);
       /* RedisPlugin redis = new RedisPlugin("redis", "localhost",7777,"hkzh713p@ssw0rd2");
        redis.getJedisPoolConfig()
        me.add(redis);*/
    }
    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new AuthInterceptor());
    }
    @Override
    public void configHandler(Handlers me) {
        me.add(new FileHandler());
    }
    public static DruidPlugin createDruidPlugin() {
        return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    }
}